package webjoch.misc;

import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

import webjoch.wrappers.Door;
import webjoch.wrappers.Room;

/**
 * 
 * @author Webjoch
 *
 */
public class Functions{
	public static boolean inArray(int[] array, int value){
		for (int possible : array){
			if (possible == value) return true;
		}
		return false;
	}
	
	public static boolean inArray(int[][] array, int value){
		for (int[] possibleArray : array){
			for (int possible : possibleArray){
				if (possible == value) return true;
			}
		}
		return false;
	}
	
	public static boolean inArray(Door[] array, Door value){
	    if (value.getObject() == null) return false;
	    
		for (Door door : array){
		    if (door.getObject() == null) continue;
			if (value.getObject().getId() == door.getObject().getId()) return true;
		}
		return false;
	}
	
	public static boolean inArray(Object[] array, Object value){
	    for (Object current : array){
	        if (value.equals(current)) return true;
	    }
	    return false;
	}
	
	public static Npc getNearest(Npc[] array) {
	    int closestInt = 99999;
	    Npc closestNpc = null;
	    
	    for (int i=0; i<array.length; i++) {
	        int dist = Calculations.distanceTo(array[i]);
	        if (dist < closestInt) {
	            closestInt = dist;
	            closestNpc = array[i];
	        }
	    }
	    return closestNpc;
	}
	
	public static Tile roomLocationToTile(Room room, int[] roomLocation){
		Tile firstTile = null;
		switch(room.getRotation()){
		case 0: 
			firstTile = room.getArea().getTileArray()[15]; 
			return new Tile(firstTile.getX() - roomLocation[1] + 15, firstTile.getY() + roomLocation[0] - 15);
			
		case 2: 
			firstTile = room.getArea().getTileArray()[255]; 
			return new Tile(firstTile.getX() + roomLocation[1] - 15, firstTile.getY() - roomLocation[0]);
			
		case 3: 
			firstTile = room.getArea().getTileArray()[240];
			return new Tile(firstTile.getX() - roomLocation[0], firstTile.getY() - roomLocation[1] + 15);
			
		default: 
			firstTile = room.getArea().getTileArray()[0]; 
			break;
		}
		int x = firstTile.getX() + roomLocation[0];
		int y = firstTile.getY() + roomLocation[1];
		return new Tile(x, y);
	}
}
